<template>
  <div class="elderly-container">
    <!-- 头部卡片 -->
    <el-card class="header-card" shadow="hover">
      <div class="header-content">
        <div class="title-section">
          <div class="title-icon">
            <i class="fas fa-id-card-alt"></i>
          </div>
          <div>
            <h1 class="header-title">客户信息档案管理</h1>
            <p class="header-subtitle">老人信息登记、健康状态跟踪与管理</p>
          </div>
        </div>
        <div class="header-buttons">
          <el-button type="primary" @click="refreshData" class="query-button">
            <i class="fas fa-sync-alt"></i>
            刷新数据
          </el-button>
          <el-button type="success" @click="handleAdd" class="add-button">
            <i class="fas fa-user-plus"></i>
            新增客户
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 过滤区域 -->
    <el-card class="filter-card" shadow="hover">
      <div class="filter-content">
        <div class="filter-group">
          <el-input 
            v-model="queryPageParam.name" 
            placeholder="请输入老人姓名" 
            clearable
            class="filter-input"
          >
            <template #prefix>
              <i class="fas fa-user"></i>
            </template>
          </el-input>
        </div>
        
        <div class="filter-group">
          <el-select 
            v-model="queryPageParam.healthStatus" 
            placeholder="健康状态"
            clearable
            class="filter-input"
          >
            <el-option label="良好" value="1"></el-option>
            <el-option label="一般" value="2"></el-option>
            <el-option label="较差" value="3"></el-option>
            <el-option label="病重" value="4"></el-option>
          </el-select>
        </div>
        
        <div class="filter-group">
          <el-select 
            v-model="queryPageParam.gender" 
            placeholder="选择性别"
            clearable
            class="filter-input"
          >
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
          </el-select>
        </div>
        
        <div class="filter-group">
          <el-button type="primary" @click="handleQueryPage" class="search-button">
            <i class="fas fa-search"></i>
            查询记录
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card" shadow="hover">
      <el-table :data="tableData" style="width: 100%" stripe v-loading="loading">
        <el-table-column prop="elderlyId" label="编号" width="100" align="center">
          <template #header>
            <i class="fas fa-hashtag"></i>
            <span>编号</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="name" label="姓名" width="180" align="center">
          <template #header>
            <i class="fas fa-user"></i>
            <span>姓名</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="gender" label="性别" width="120" align="center">
          <template #header>
            <i class="fas fa-venus-mars"></i>
            <span>性别</span>
          </template>
          <template v-slot="scope">
            <span :class="'gender-tag gender-' + (scope.row.gender == 1 ? 'male' : 'female')">
              <i :class="scope.row.gender == 1 ? 'fas fa-mars' : 'fas fa-venus'"></i>
              {{ genderMap[scope.row.gender] }}
            </span>
          </template>
        </el-table-column>
        
        <el-table-column prop="age" label="年龄" width="120" align="center">
          <template #header>
            <i class="fas fa-birthday-cake"></i>
            <span>年龄</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="healthStatus" label="健康状态" width="150" align="center">
          <template #header>
            <i class="fas fa-heartbeat"></i>
            <span>健康状态</span>
          </template>
          <template v-slot="scope">
            <span :class="'status-tag status-' + getHealthStatusClass(scope.row.healthStatus)">
              <i :class="getHealthStatusIcon(scope.row.healthStatus)"></i>
              {{ statusMap[scope.row.healthStatus] }}
            </span>
          </template>
        </el-table-column>
        
        <el-table-column prop="createTime" label="创建时间" width="200" align="center">
          <template #header>
            <i class="fas fa-calendar-plus"></i>
            <span>创建时间</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="updateTime" label="更新时间" width="200" align="center">
          <template #header>
            <i class="fas fa-calendar-check"></i>
            <span>更新时间</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" fixed="right" width="280" align="center">
          <template #default="scope">
            <div class="action-cell">
              <el-button 
                size="small" 
                class="action-btn view-btn"
                @click="handleDetail(scope.row.elderlyId)"
              >
                <i class="fas fa-eye"></i>
                查看
              </el-button>
              <el-button 
                type="primary" 
                size="small" 
                class="action-btn edit-btn"
                @click="handleEdit(scope.row.elderlyId)"
              >
                <i class="fas fa-edit"></i>
                编辑
              </el-button>
              <el-popconfirm 
                title="确定要删除此客户信息吗？" 
                confirm-button-text="删除"
                cancel-button-text="取消"
                @confirm="handleDeleteConfirm(scope.row.elderlyId)"
              >
                <template #reference>
                  <el-button 
                    type="danger" 
                    size="small" 
                    class="action-btn delete-btn"
                  >
                    <i class="fas fa-trash"></i>
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination 
          v-model:current-page="queryPageParam.pageNo" 
          v-model:page-size="queryPageParam.pageSize"
          :page-sizes="[5, 10, 20, 30]" 
          layout="total, sizes, prev, pager, next, jumper" 
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { page, deleteById } from "@/api/elderInfo";
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus';

const router = useRouter();
const queryPageParam = reactive({
  name: '',
  healthStatus: null,
  gender: null,
  pageNo: 1,
  pageSize: 5
});

const genderMap = reactive({
  1: '男',
  0: '女'
});

const statusMap = reactive({
  1: '良好',
  2: '一般', 
  3: '较差',
  4: '病重'
});

// 数据
const tableData = ref([]);
const total = ref(0);
const loading = ref(false);

const handleQueryPage = () => {
  loading.value = true;
  page(queryPageParam).then(res => {
    console.log(res);
    tableData.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
    ElMessage.error('数据加载失败');
  });
};
// 刷新数据函数（新增）
const refreshData = () => {
  // 显示加载状态
  loading.value = true;
  
  // 可以选择是否重置查询条件（以下为示例，可根据需求调整）
  const isReset = true;
  if (isReset) {
    // 重置查询条件为默认值
    queryPageParam.name = '';
    queryPageParam.healthStatus = null;
    queryPageParam.gender = null;
    queryPageParam.pageNo = 1;
  } else {
    // 保持当前查询条件，只刷新数据
    // 无需额外操作
  }
  
  // 调用查询函数
  page(queryPageParam).then(res => {
    console.log('刷新数据成功', res);
    tableData.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
    ElMessage.success('数据刷新成功');
  }).catch(error => {
    console.error('刷新数据失败', error);
    loading.value = false;
    ElMessage.error('数据刷新失败，请重试');
  });
};
// 修改每页数量的回调函数
const handleSizeChange = (sizeValue) => {
  queryPageParam.pageSize = sizeValue;
  handleQueryPage();
};

// 修改页码之后的回调函数
const handleCurrentChange = (currentValue) => {
  queryPageParam.pageNo = currentValue;
  handleQueryPage();
};

// 查看
const handleDetail = (id) => {
  router.push({
    path: '/elderInfo/detail',
    query: { id }
  });
};

// 编辑
const handleEdit = (id) => {
  router.push({
    path: '/elderInfo/save',
    query: { id }
  });
};

// 确认删除
const handleDeleteConfirm = (id) => {
  deleteById(id).then(() => {
    ElMessage.success('删除成功');
    handleQueryPage(); // 删除成功后再次分页查询
  }).catch(() => {
    ElMessage.error('删除失败');
  });
};

// 新增
const handleAdd = () => {
  router.push({
    path: '/elderInfo/save'
  });
};

// 获取健康状态样式类
const getHealthStatusClass = (status) => {
  const map = {
    1: 'good',
    2: 'normal',
    3: 'poor',
    4: 'critical'
  };
  return map[status] || 'normal';
};

// 获取健康状态图标
const getHealthStatusIcon = (status) => {
  const map = {
    1: 'fas fa-smile',
    2: 'fas fa-meh',
    3: 'fas fa-frown',
    4: 'fas fa-procedures'
  };
  return map[status] || 'fas fa-question-circle';
};

// Vue 挂载完成之后执行钩子函数
onMounted(() => {
  handleQueryPage();
});
</script>

<style scoped>
.elderly-container {
  padding: 20px;
  background: linear-gradient(135deg, #f8f9fc 0%, #eef2f7 100%);
  min-height: 100vh;
  max-width: 1600px;
  margin: 0 auto;
}

.header-card {
  border-radius: 18px;
  margin-bottom: 20px;
  background: linear-gradient(135deg, #ffffff 0%, #f8fbfe 100%);
  border: none;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
}

.title-section {
  display: flex;
  align-items: center;
  gap: 15px;
}

.title-icon {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #4a90e2 0%, #63b3ed 100%);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(74, 144, 226, 0.25);
}

.title-icon i {
  font-size: 28px;
  color: white;
}

.header-title {
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
  letter-spacing: 0.5px;
  margin: 0;
}

.header-subtitle {
  font-size: 15px;
  color: #7f8c8d;
  margin-top: 5px;
}

.header-buttons {
  display: flex;
  gap: 15px;
}

.query-button, .add-button {
  padding: 10px 24px;
  border-radius: 12px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  border: none;
  font-size: 14px;
}

.query-button:hover, .add-button:hover {
  transform: translateY(-2px);
}

.query-button {
  background: linear-gradient(135deg, #4a90e2 0%, #63b3ed 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(74, 144, 226, 0.3);
}

.query-button:hover {
  box-shadow: 0 6px 16px rgba(74, 144, 226, 0.4);
}

.add-button {
  background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(39, 174, 96, 0.3);
}

.add-button:hover {
  box-shadow: 0 6px 16px rgba(39, 174, 96, 0.4);
}

.filter-card {
  border-radius: 18px;
  margin-bottom: 20px;
  background: white;
  border: none;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
}

.filter-content {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  padding: 15px;
}

.filter-group {
  flex: 1;
  min-width: 200px;
}

.filter-input, .search-button {
  width: 100%;
}

.search-button {
  padding: 10px 15px;
  border-radius: 12px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.table-card {
  border-radius: 18px;
  background: white;
  border: none;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
  padding: 20px;
}

.el-table {
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid #eef2f7;
}

.el-table th {
  background-color: #f8fafd !important;
  font-weight: 600 !important;
  color: #2c3e50 !important;
}

.el-table__row:hover td {
  background-color: #f5f9ff !important;
}

.action-cell {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 7px 14px !important;
  border-radius: 10px !important;
  font-size: 13px !important;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
}

.view-btn {
  background: #e8f4ff;
  color: #3498db;
  border: 1px solid #c5e1ff;
}

.edit-btn {
  background: #e8f7f0;
  color: #27ae60;
  border: 1px solid #c5ebd6;
}

.delete-btn {
  background: #fff0f0;
  color: #e74c3c;
  border: 1px solid #ffd1d1;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

/* 状态标签 */
.status-tag {
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.status-good { background: #e8f7f0; color: #27ae60; }
.status-normal { background: #e8f4ff; color: #3498db; }
.status-poor { background: #fff8e6; color: #f39c12; }
.status-critical { background: #fff0f0; color: #e74c3c; }

.gender-tag {
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.gender-male { background: #e8f4ff; color: #3498db; }
.gender-female { background: #f9f0ff; color: #9b59b6; }

/* 响应式设计 */
@media (max-width: 992px) {
  .header-content {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }
  
  .title-section {
    justify-content: center;
  }
  
  .header-buttons {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 768px) {
  .filter-content {
    flex-direction: column;
  }
  
  .filter-group {
    min-width: 100%;
  }
  
  .action-cell {
    flex-wrap: wrap;
    justify-content: center;
  }
}
</style>